//
//  UIView+RSRect.m
//  RSTools
//
//  Created by thinkjoy on 2017/9/12.
//  Copyright © 2017年 杜瑞胜. All rights reserved.
//

#import "UIView+RSRect.h"

@implementation UIView (RSRect)

- (CGPoint)viewOrigin {
    return self.frame.origin;
}
- (void)setViewOrigin:(CGPoint)viewOrigin {
    CGRect newFrame = self.frame;
    newFrame.origin = viewOrigin;
    self.frame      = newFrame;
}

- (CGSize)viewSize {
    return self.frame.size;
}
- (void)setViewSize:(CGSize)viewSize {
    CGRect newFrame = self.frame;
    newFrame.size   = viewSize;
    self.frame      = newFrame;
}

- (CGFloat)minX {
    return self.frame.origin.x;
}
- (void)setMinX:(CGFloat)minX {
    CGRect newFrame   = self.frame;
    newFrame.origin.x = minX;
    self.frame        = newFrame;
}

- (CGFloat)minY {
    return self.frame.origin.y;
}
- (void)setMinY:(CGFloat)minY {
    CGRect newFrame   = self.frame;
    newFrame.origin.y = minY;
    self.frame        = newFrame;
}

- (CGFloat)width {
    return CGRectGetWidth(self.bounds);
}
- (void)setWidth:(CGFloat)width {
    
    CGRect newFrame     = self.frame;
    newFrame.size.width = width;
    self.frame          = newFrame;
}

- (CGFloat)height {
    return CGRectGetHeight(self.bounds);
}
- (void)setHeight:(CGFloat)height {
    CGRect newFrame      = self.frame;
    newFrame.size.height = height;
    self.frame           = newFrame;
}

- (CGFloat)maxX{
    return CGRectGetMaxX(self.bounds);
}
- (void)setMaxX:(CGFloat)maxX
{
    UIView * superV =   self.superview;
    
    maxX    =   MIN(maxX, superV.frame.size.width); //需小于父视图宽度
    maxX    =   MAX(maxX, self.frame.origin.x);     //需大于当前View的X值
    CGRect newFrame =   self.frame;
    CGFloat width   =   maxX - self.frame.origin.x;
    newFrame.size.width    =   width<0?0:width;;
    self.frame        = newFrame;
}

- (CGFloat)maxY{
    return CGRectGetMaxY(self.bounds);
}
- (void)setMaxY:(CGFloat)maxY
{
    UIView * superV =   self.superview;
    
    maxY    =   MIN(maxY, superV.frame.size.height); //需小于父视图高度
    maxY    =   MAX(maxY, self.frame.origin.y);      //需大于当前View的Y值
    CGRect newFrame =   self.frame;
    CGFloat height  =   maxY - self.frame.origin.y;
    newFrame.size.height    =   height<0?0:height;;
    self.frame  =   newFrame;
}

- (CGFloat)top {
    return self.frame.origin.y;
}
- (void)setTop:(CGFloat)top {
    CGRect newFrame   = self.frame;
    newFrame.origin.y = top;
    self.frame        = newFrame;
}

- (CGFloat)bottom {
    return self.frame.origin.y + self.frame.size.height;
}
- (void)setBottom:(CGFloat)bottom {
    UIView * superV =   self.superview;
    
    CGRect newFrame =   self.frame;
    CGFloat height  =   superV.bounds.size.height - self.frame.origin.y -bottom;
    newFrame.size.height    =   height<0?0:height;;
    self.frame        = newFrame;
}

- (CGFloat)left {
    return self.frame.origin.x;
}
- (void)setLeft:(CGFloat)left {
    CGRect newFrame   = self.frame;
    newFrame.origin.x = left;
    self.frame        = newFrame;
}

- (CGFloat)right {
    return self.frame.origin.x + self.frame.size.width;
}
- (void)setRight:(CGFloat)right {
    UIView * superV =   self.superview;
    
    CGRect newFrame =   self.frame;
    CGFloat width   =   superV.bounds.size.width - self.frame.origin.x -right;
    newFrame.size.width    =   width<0?0:width;;
    self.frame        = newFrame;
}

- (CGFloat)centerX {
    return self.center.x;
}
- (void)setCenterX:(CGFloat)centerX {
    CGPoint newCenter = self.center;
    newCenter.x       = centerX;
    self.center       = newCenter;
}

- (CGFloat)centerY {
    return self.center.y;
}
- (void)setCenterY:(CGFloat)centerY {
    CGPoint newCenter = self.center;
    newCenter.y       = centerY;
    self.center       = newCenter;
}

@end
